﻿using GKQC.Common;
using GKQC.DbHelper.DbModels;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.Util;
using NPOI.XSSF.UserModel;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace GKQC.Ort.Reports
{
    /// <summary>
    /// 信赖性试验：无載 检测5个 有BOM
    /// </summary>
    public class R019: BaseReport
    {
        public override void GenReportSheet(IWorkbook workbook, ISheet sheet, OrtHeadModel head)
        {
            #region 表头
            int headColumnsIndex = 0;
            
            for (int i = 0; i < 1; i++)
            {
                try
                {
                    sheet.GetRow(1).GetCell(headColumnsIndex + 33).SetCellValue(head.LcMax);

                    //1.Customer:
                    if (sheet.GetRow(2).GetCell(headColumnsIndex + 2) != null)
                    sheet.GetRow(2).GetCell(headColumnsIndex+2).SetCellValue(head.CustomerCode);
                    sheet.GetRow(2).GetCell(headColumnsIndex+9).SetCellValue(head.LotNo);
                    sheet.GetRow(2).GetCell(headColumnsIndex + 16).SetCellValue(head.Series);
                    sheet.GetRow(2).GetCell(headColumnsIndex + 22).SetCellValue(head.Cap);
                    sheet.GetRow(2).GetCell(headColumnsIndex + 24).SetCellValue(head.Voltage);
                    sheet.GetRow(2).GetCell(headColumnsIndex + 29).SetCellValue(head.Size);
                    
                    sheet.GetRow(2).GetCell(headColumnsIndex + 32).SetCellValue(head.Rubber);
                    
                    


                    sheet.GetRow(3).GetCell(headColumnsIndex + 3).SetCellValue(head.FoilP);   //正箔
                    sheet.GetRow(3).GetCell(headColumnsIndex + 9).SetCellValue(head.FoilN);   //负箔
                    sheet.GetRow(3).GetCell(headColumnsIndex + 16).SetCellValue(head.Eps);  //电解纸
                    sheet.GetRow(3).GetCell(headColumnsIndex + 23).SetCellValue(head.Paste);  //电解质
                    sheet.GetRow(3).GetCell(headColumnsIndex + 29).SetCellValue(head.DateCode);  //
                    sheet.GetRow(3).GetCell(headColumnsIndex + 32).SetCellValue(head.FoilpSupplier);
                    sheet.GetRow(3).GetCell(headColumnsIndex + 35).SetCellValue(head.Pn);
                    sheet.GetRow(3).GetCell(headColumnsIndex + 36).SetCellValue(head.CustomerCode);

                    sheet.GetRow(4).GetCell(headColumnsIndex + 2).SetCellValue(head.TestVoltage);
                    sheet.GetRow(4).GetCell(headColumnsIndex + 15).SetCellValue(head.TestTemp);
                    sheet.GetRow(4).GetCell(headColumnsIndex + 22).SetCellValue(head.TestTotalTime);

                    sheet.GetRow(5).GetCell(headColumnsIndex + 49).SetCellValue(head.SampleDate.ToString("yyyy/M/d"));
                    //sheet.GetRow(5).GetCell(headColumnsIndex + 50).SetCellValue(head.TestDate.ToString("yyyy/M/d"));
                    //sheet.GetRow(5).GetCell(headColumnsIndex + 51).SetCellValue(head.TestDate.ToString("yyyy/M/d"));



                    sheet.GetRow(14).GetCell(headColumnsIndex + 6).SetCellValue(head.Remark);

                    //MAX:+25%  MIN:-25%
                    if (sheet.GetRow(21) == null) sheet.CreateRow(21);
                    if (sheet.GetRow(21).GetCell(1) == null) sheet.GetRow(21).CreateCell(1);
                    sheet.GetRow(21).GetCell(1).SetCellValue(
                        string.Format("MAX:+{0}%  MIN:-{0}%", head.CapRate.ToString()));
                    //MAX:18%
                    if (sheet.GetRow(22) == null) sheet.CreateRow(22);
                    if (sheet.GetRow(22).GetCell(1) == null) sheet.GetRow(22).CreateCell(1);
                    sheet.GetRow(22).GetCell(1).SetCellValue(
                        string.Format("less than:{0}", head.DfMaxAfter / 100d));
                    //MAX:352uA
                    if (sheet.GetRow(23) == null) sheet.CreateRow(23);
                    if (sheet.GetRow(23).GetCell(1) == null) sheet.GetRow(23).CreateCell(1);
                    sheet.GetRow(23).GetCell(1).SetCellValue(
                        string.Format("less than:{0}uA", head.LcMax.ToString()));

                    //Tested by:王燕
                    sheet.GetRow(33).GetCell(headColumnsIndex + 24).SetCellValue("Tested by:" + head.CreateUser);
                }
                catch (Exception ex)
                {
                    Logger.LogError(ex);
                }


                headColumnsIndex += 29;
            }
            #endregion 表头

            #region 细表
            int currentDataColumnIndex = 1;
            int dataRowIndex = 8;
            int currentDtlColumnIndex = 1;
            int currentHeadRowIndex = 5;
            int dtlColumns = 5;
            int dtlRowsCount = 9;

            string RT = "RT:";
            string RH= "RH:";

            for (int i = 0; i < head.Dtls.Count; i++)
            {
                if (i > 5) break;

                try
                {
                    sheet.GetRow(currentHeadRowIndex).GetCell(currentDtlColumnIndex).SetCellValue(
                        head.Dtls[i].TestTime.ToString()+ "hr");
                    sheet.GetRow(currentHeadRowIndex).GetCell(currentDtlColumnIndex+2).SetCellValue(
                        head.Dtls[i].TestDate.ToString("yyyy/M/d"));

                    //RT: 
                    sheet.GetRow(currentHeadRowIndex + 1).GetCell(currentDtlColumnIndex).SetCellValue(RT);
                    sheet.GetRow(currentHeadRowIndex+1).GetCell(currentDtlColumnIndex+1).SetCellValue(
                        head.Dtls[i].GetTemp());
                    sheet.GetRow(currentHeadRowIndex + 1).GetCell(currentDtlColumnIndex + 2).SetCellValue(RH);
                    sheet.GetRow(currentHeadRowIndex + 1).GetCell(currentDtlColumnIndex + 3).SetCellValue(
                        head.Dtls[i].GetHumidity());

                    //CAP(uF)	^C/C(%)	DF(%)	△DF(%)	LC(uA)
                    if (i>0)
                    {
                        sheet.GetRow(currentHeadRowIndex+2).GetCell(currentDtlColumnIndex).SetCellValue("CAP(uF)");
                        sheet.GetRow(currentHeadRowIndex + 2).GetCell(currentDtlColumnIndex+1).SetCellValue("^C/C(%)");
                        sheet.GetRow(currentHeadRowIndex + 2).GetCell(currentDtlColumnIndex+2).SetCellValue("tanδ");
                        sheet.GetRow(currentHeadRowIndex + 2).GetCell(currentDtlColumnIndex+3).SetCellValue("△tanδ");
                        sheet.GetRow(currentHeadRowIndex + 2).GetCell(currentDtlColumnIndex+4).SetCellValue("LC(uA)");
                    }

                    if (sheet.GetRow(54).GetCell(i + 1) == null) sheet.GetRow(54).CreateCell(i + 1);
                    sheet.GetRow(54).GetCell(i + 1).SetCellValue(head.Dtls[i].TestTime + "hr");
                    if (sheet.GetRow(60).GetCell(i + 1) == null) sheet.GetRow(60).CreateCell(i + 1);
                    sheet.GetRow(60).GetCell(i + 1).SetCellValue(head.Dtls[i].TestTime + "hr");
                    if (sheet.GetRow(66).GetCell(i + 1) == null) sheet.GetRow(66).CreateCell(i + 1);
                    sheet.GetRow(66).GetCell(i + 1).SetCellValue(head.Dtls[i].TestTime + "hr");
                }
                catch (Exception ex)
                {
                    Logger.LogError(ex);
                }

                #region data
                int cIndex = currentDataColumnIndex;
                var caps = head.Dtls[i].Datas.Where(d => d.ItemCode == "CAP");
                if (caps.Any())
                {
                    var carpss = caps.ToList();
                    ICellStyle lcCellStyle = workbook.CreateCellStyle();
                    lcCellStyle.CloneStyleFrom(sheet.GetRow(dataRowIndex).GetCell(cIndex).CellStyle);
                    IDataFormat lcdataformat = workbook.CreateDataFormat();
                    double maxlc = caps.Max(t => t.Value);
                    lcCellStyle.DataFormat = lcdataformat.GetFormat("0.00");
                    //int capNum = OrtReportFactory.GetCapFormatNum(maxlc);
                    for (int k = 0; k < carpss.Count; k++)
                    {
                        if (k >= head.ElectricQty) break;
                        if (sheet.GetRow(dataRowIndex + k) == null || sheet.GetRow(dataRowIndex + k).GetCell(cIndex) == null)
                            continue;
                        if(carpss[k].Value!= 0) 
                            sheet.GetRow(dataRowIndex + k).GetCell(cIndex).SetCellValue(carpss[k].Value);
                        sheet.GetRow(dataRowIndex + k).GetCell(cIndex).CellStyle = lcCellStyle;
                    }
                    sheet.GetRow(dataRowIndex + head.ElectricQty).GetCell(cIndex).CellStyle = lcCellStyle;
                }

                if (i > 0) cIndex += 2;
                else cIndex++;
                var dfs = head.Dtls[i].Datas.Where(d => d.ItemCode == "DF");
                if (dfs.Any())
                {
                    var dfss = dfs.ToList();
                    for (int k = 0; k < dfss.Count; k++)
                    {
                        if (k >= head.ElectricQty) break;
                        if (sheet.GetRow(dataRowIndex + k) == null || sheet.GetRow(dataRowIndex + k).GetCell(cIndex) == null)
                            continue;
                        if (dfss[k].Value != 0)
                            sheet.GetRow(dataRowIndex + k).GetCell(cIndex).SetCellValue(Math.Round(dfss[k].Value / 100, 4));
                    }
                }
                if (i == 0) cIndex++;

                if (i > 0) cIndex += 2;
                else cIndex++;
                var lcs = head.Dtls[i].Datas.Where(d => d.ItemCode == "LC");
                if (lcs.Any())
                {
                    var lcss = lcs.ToList();
                    ICellStyle lcCellStyle = workbook.CreateCellStyle();
                    lcCellStyle.CloneStyleFrom(sheet.GetRow(dataRowIndex).GetCell(cIndex).CellStyle);
                    IDataFormat lcdataformat = workbook.CreateDataFormat();
                    double maxlc = caps.Max(t => t.Value);
                    lcCellStyle.DataFormat = lcdataformat.GetFormat(OrtReportFactory.GetLcNumFormat(maxlc));
                    //int lcNum = OrtReportFactory.GetLcFormatNum(maxlc);
                    for (int k = 0; k < lcss.Count; k++)
                    {
                        if (k >= head.ElectricQty) break;
                        if (sheet.GetRow(dataRowIndex + k) == null || sheet.GetRow(dataRowIndex + k).GetCell(cIndex) == null)
                            continue;
                        if (lcss[k].Value != 0)
                            sheet.GetRow(dataRowIndex + k).GetCell(cIndex).SetCellValue(lcss[k].Value);
                        sheet.GetRow(dataRowIndex + k).GetCell(cIndex).CellStyle = lcCellStyle;
                    }
                    sheet.GetRow(dataRowIndex + head.ElectricQty).GetCell(cIndex).CellStyle = lcCellStyle;
                }

                #endregion data

                currentDtlColumnIndex += dtlColumns;
                currentDataColumnIndex += dtlColumns;

                if (i > 0 && (i + 1) % 6 == 0)
                {
                    currentDtlColumnIndex=1;
                    currentDataColumnIndex=1;
                    currentHeadRowIndex += dtlRowsCount;
                    dataRowIndex += dtlRowsCount;
                }
            }

            #endregion

            //int headCount = (int)(Math.Ceiling(head.Dtls.Count / 6d));
            //int firstDataRow = 5;
            //if (headCount < 5)
            //{
            //    int lastDataRowIndex = firstDataRow + dtlRowsCount * 5;
            //    int firstHideRowIndex = firstDataRow + (headCount > 1 ? headCount : 2) * dtlRowsCount;
            //    for (int i= firstHideRowIndex; i< lastDataRowIndex-1;i++)
            //    {
            //        sheet.GetRow(i).ZeroHeight = true ;
            //    }
            //}

            sheet.ForceFormulaRecalculation = true;
        }
    }
}
